package cn.blunce.view_utils.ChartHelp;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Paint.Align;
import android.view.View;

import org.achartengine.ChartFactory;
import org.achartengine.chart.PointStyle;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.model.XYSeries;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Blunce on 2015/11/22 0022.
 */
public class MyChartHelper {

    public static View excuteView(Context context, String data) {
        XYMultipleSeriesDataset dataSet = myBuildDataSet(data);
        XYMultipleSeriesRenderer render = myBuildRenderer(dataSet);
        return ChartFactory.getLineChartView(context, dataSet, render);
    }

    /**
     * 创建x轴和y轴的数据
     *
     * @param data
     * @return
     */
    private static XYMultipleSeriesDataset myBuildDataSet(String data) {
        // TODO Auto-generated method stub
        String[] strings = data.split(",");

        String[] titles = new String[]{"数据"};

        List<double[]> XValues = new ArrayList<double[]>();
        double[] Xvalue = new double[strings.length];
        for (int i = 0; i < Xvalue.length; i++)
            Xvalue[i] = i;
        XValues.add(Xvalue);

        List<double[]> Yvalues = new ArrayList<double[]>();
        double[] Yvalue = new double[strings.length];
        for (int i = 0; i < strings.length; i++)
            Yvalue[i] = Double.valueOf(strings[i]);
        Yvalues.add(Yvalue);

        XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();

        for (int i = 0; i < titles.length; i++) {
            XYSeries series = new XYSeries(titles[i], 0);
            double[] xV = XValues.get(i);
            double[] yV = Yvalues.get(i);
            for (int k = 0; k < xV.length; k++) {
                series.add(xV[k], yV[k]);
            }
            dataset.addSeries(series);
        }

        return dataset;
    }

    /**
     * 设置图表属性
     *
     * @param dataSet
     * @return
     */
    private static XYMultipleSeriesRenderer myBuildRenderer(
            XYMultipleSeriesDataset dataSet) {
        // TODO Auto-generated method stub
        XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();

        renderer.setAxisTitleTextSize(16);
        renderer.setChartTitleTextSize(20);
        renderer.setLabelsTextSize(15);
        renderer.setLegendTextSize(15);
        renderer.setPointSize(5f);
        renderer.setMargins(new int[]{20, 30, 15, 20});

        int[] colors = new int[]{Color.BLUE};
        PointStyle[] styles = new PointStyle[]{PointStyle.CIRCLE};
        for (int i = 0; i < colors.length; i++) {
            XYSeriesRenderer r = new XYSeriesRenderer();
            r.setColor(colors[i]);
            r.setPointStyle(styles[i]);
            renderer.addSeriesRenderer(r);
        }

        XYSeries serie = dataSet.getSeriesAt(0);
        double minY = serie.getMinY();
        double maxY = serie.getMaxY();
        double split_dis = (maxY - minY) / 10;
        if (split_dis - 0 < 0.0001) {
            split_dis = 50;
        }

        for (int i = 0; i < colors.length; i++) {
            renderer.getSeriesRendererAt(i).setDisplayChartValues(true);
            renderer.getSeriesRendererAt(i).setChartValuesTextSize(20);
            renderer.getSeriesRendererAt(i).setChartValuesTextAlign(Align.CENTER);
        }

        renderer.setXAxisMin(-5);
        renderer.setXAxisMax(15);
        renderer.setYAxisMin(minY - split_dis);
        renderer.setYAxisMax(maxY + split_dis);
        renderer.setExternalZoomEnabled(false);
        renderer.setZoomEnabled(false, false);
        renderer.setClickEnabled(true);
        renderer.setZoomRate(1.1f);// 放大的倍率
        renderer.setMarginsColor(Color.WHITE);
        return renderer;
    }
}
